class PIDController:
    def __init__(self, kp=0, ki=0, kd=0, lim_up=0, lim_low=0):
        self.kp = kp
        self.ki = ki
        self.kd = kd

        self.sigma_err = 0
        self.last_err = 0

        self.lim_up = lim_up
        self.lim_low = lim_low

    def output(self, required, feedback):
        err = required - feedback

        self.sigma_err += err
        p_term = self.kp * err
        i_term = self.ki * self.sigma_err
        d_term = self.kd * (err - self.last_err)
        self.last_err = err

        out = p_term + i_term + d_term

        if out > self.lim_up:
            out = self.lim_up
        elif out < self.lim_low:
            out = self.lim_low

        return out

